package com.bjsxt.commom.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bjsxt.commom.util.DateFormatUtil;
import com.bjsxt.commom.util.PageInfoUtil;

public class BasicController
{
	/* request/response */
	public static  HttpServletRequest request = null;
	public static HttpServletResponse response = null;
	
	@Resource
	private PageInfoUtil pageInfoUtil ;
	@Resource
	private DateFormatUtil dateFormatUtil;
	
	/**
	 * 常用参数的处理
	 * @param rquest
	 * @return Map:sourceMap返回原参数Map集合   condMap返回查询参数Map集合  pageInfoUtil:返回pageInfoUtil
	 */
	protected Map<String,Object> getCondMap(HttpServletRequest request)
	{
		Map<String,Object> resultMap = new HashMap<String,Object>();
		/* 分页信息的处理 */
		String pageSize = request.getParameter("pageSize");
		String currentPage = request.getParameter("currentPage");
		
		if(!"".equalsIgnoreCase(pageSize) && pageSize != null)
		{
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		}
		if(!"".equalsIgnoreCase(currentPage) && currentPage != null)
		{
			pageInfoUtil.setPageSize(Integer.valueOf(currentPage));
		}
		resultMap.put("pageInfoUtil", pageInfoUtil);
		/* 原参数信息处理 */
		String status = request.getParameter("status");
		String keyword = request.getParameter("keyword");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		Map<String,Object> sourceMap = new HashMap<String, Object>();
		sourceMap.put("status", status);
		sourceMap.put("keyword", keyword);
		sourceMap.put("startTime", startTime);
		sourceMap.put("endTime", endTime);
		resultMap.put("sourceMap", sourceMap);
		
		/* 查询参数信息处理 */
		Map<String,Object> condMap = new HashMap<String,Object>();
		if(!"".equalsIgnoreCase(status) && status != null)
		{
			condMap.put("status", Byte.valueOf(status));
		}
		if(!"".equalsIgnoreCase(keyword) && keyword != null)
		{
			condMap.put("keyword", "%"+keyword+"%");
		}
		if(!"".equalsIgnoreCase(startTime) && startTime != null
				&& !"".equalsIgnoreCase(endTime) && endTime != null)
		{
			Date start = dateFormatUtil.parseStr(startTime);
			Date end = dateFormatUtil.parseStr(endTime);
			if(start!=null && end!=null)
			{
				condMap.put("startTime", start);
				condMap.put("endTime", end);
			}
		}
		resultMap.put("condMap", condMap);
		return resultMap;
	}

}
